//
//  CustomerfilterView.m
//  New_Runway
//
//  Created by mack on 2018/2/24.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CustomerfilterView.h"
#import "CrmCustomerLevelModel.h"
#import "CustomersModel.h"
@interface CustomerfilterView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *ArrayId;
@property (nonatomic, assign) NSInteger index;

@end

@implementation CustomerfilterView

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
-(NSMutableArray *)ArrayId{
    if (!_ArrayId) {
        _ArrayId = [[NSMutableArray alloc] init];
    }
    return _ArrayId;
}
-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
        [self initTableview];
    }
    return self;
}

-(void)initTableview{
    //    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        //        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self addSubview:self.tableview];

    self.tableview.frame = CGRectMake(0, 0, Screen_width, 0);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = UIColorHex(0xf5f5f5);
    line.frame = CGRectMake(0, 0, Screen_width, 0.5);
    [cell.contentView addSubview:line];
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.textLabel.font = kFontSize(12);
    cell.tintColor = MAIN_NAV_COLOR();

    if (_selIndex == indexPath) {
        cell.textLabel.textColor = MAIN_NAV_COLOR();
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.textColor = MAIN_FONT_COLOR();

    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *celled = [tableView cellForRowAtIndexPath:_selIndex];
    celled.accessoryType = UITableViewCellAccessoryNone;
    celled.textLabel.textColor = MAIN_FONT_COLOR();
    //记录当前选中的位置索引
    _selIndex = indexPath;
    //当前选择的打勾
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.textLabel.textColor = MAIN_NAV_COLOR();
    NSString *stypeID = self.ArrayId[indexPath.row];
    self.selectedBlock(stypeID, self.index, indexPath);
    [self defaultViewAnimation:NO];
    
    
    
    
}
-(void)reloadFilterview:(NSInteger)tag indexpath:(NSIndexPath *)selectedTag{
    _selIndex = selectedTag;
    self.index = tag;
    [self.dataArray removeAllObjects];
    if (tag == 0) {
        NSArray *array;
        NSArray *ID;
        if (self.type == kCRMListViewTypeNewToAssigned) {
            ID = @[@"1"];
            array =@[@"创建时间"];
        } else {
            ID = @[@"1",@"0"];
            array =@[@"创建时间",@"下次跟进时间"];
        }
        [self.ArrayId addObjectsFromArray:ID];
        [self.dataArray addObjectsFromArray:array];
        [self.tableview reloadData];

    }else if (tag == 1){
        //级别
        [self loadCustomerLevel];
        
    }else if (tag == 2){
        //来源
        [self loadCustomers];
    }else if (tag == 3){
        //状态
        NSArray *array;
        NSArray *arrayId;
        if (self.type == kCRMListViewTypeAllFollowCustomer ||
            self.type == kCRMListViewTypeMyFollowCustomer) {
            array  =@[@"全部",@"跟进中",@"已预约"];
            arrayId = @[@"",@"10",@"20"];
        }else if (self.type == kCRMListViewTypeAllEndCustomer ||
                  self.type == kCRMListViewTypeMyEndCustomer){
            array  =@[@"全部",@"已战败",@"已失效",@"已成交"];
            arrayId = @[@"",@"30",@"40",@"50"];
        }

     

        [self.ArrayId addObjectsFromArray:arrayId];
        [self.dataArray addObjectsFromArray:array];
        [self.tableview reloadData];

    }

    
}
//客户级别
-(void)loadCustomerLevel{
    NSString *url = [NSString stringWithFormat:@"%@%@",CRMContextApi,customerLevelApi];
    
    NSDictionary *dict = @{@"marketId":[NRWCarUser sharedUser].marketId,
                           @"shopId": [NRWCarUser sharedUser].shopId,
                           @"levelState": @1
                           };
    
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:dict progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        [self.dataArray addObject:@"全部"];
        [self.ArrayId addObject:@""];
        if ([response[@"code"] isEqual:@1]) {
            for (NSDictionary *dic  in response[@"result"]) {
                CrmCustomerLevelModel *model = [CrmCustomerLevelModel mj_objectWithKeyValues:dic];
                [self.ArrayId addObject:model.levelId];
                [self.dataArray addObject:model.levelTxt];
            }
        }
        
        [self.tableview reloadData];

    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}
//客户来源
-(void)loadCustomers{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",CRMContextApi,customersApi];
    
    NSDictionary *dict = @{@"marketId":[NRWCarUser sharedUser].marketId,
                           @"shopId": [NRWCarUser sharedUser].shopId,
                           @"channelState": @1
                           };
    
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:dict progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        [self.dataArray addObject:@"全部"];
        [self.ArrayId addObject:@""];
        if ([response[@"code"] isEqual:@1]) {
            NSArray *array = response[@"result"];
            for (NSDictionary *dic in array) {
                CustomersModel *model = [CustomersModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model.channelName];
                [self.ArrayId addObject:model.channelId];
                
            }
            
        }
        [self.tableview reloadData];

    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
}
-(void)defaultViewAnimation:(BOOL)isshow{
    if (isshow == YES) {
        self.frame = CGRectMake(0, self.navH + 40, Screen_width, Screen_height - self.navH - 40);
        
        [UIView animateWithDuration:0.5 animations:^{
            self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4];
            self.tableview.frame = CGRectMake(0, 0, Screen_width, 300);
        } completion:^(BOOL finished) {
            
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
            self.tableview.frame = CGRectMake(0, 0, Screen_width, 0);
            
        } completion:^(BOOL finished) {
            self.frame = CGRectMake(0, self.navH + 40, Screen_width, 0);
            
        }];
    }
    
}



@end
